package cn.agiledata.bank.linkbank.impl.validate;

import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.logic.FieldValidator;

/**
 * 数据域限制校验器。数据域值匹配条件则校验失败，用于禁止某些数据域值的提交
 * 
 * @author maxianbo
 * 
 */
public class FieldLimitationValidator implements FieldValidator {

    private boolean allowed;

    private String target;

    private String limitedPattern;

    private String errorCode;

    public String getErrorCode() {
        return this.errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getLimitedPattern() {
        return this.limitedPattern;
    }

    public String getTarget() {
        return this.target;
    }

    public boolean isAllowed() {
        return this.allowed;
    }

    public void setAllowed(boolean allowed) {
        this.allowed = allowed;
    }

    public void setLimitedPattern(String limitedPattern) {
        this.limitedPattern = limitedPattern;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public void validate(String value) throws LinkBankException {
        if (value != null) { // 为空的数据域值不做校验
            if (!isAllowed()
                    || StringUtil.matchRegex(value, getLimitedPattern())) {
                throw LinkBankException
                        .getUnallowedFieldException(getErrorCode());
            }
        }
    }

}
